package au.com.somethingwritten.controller;

import au.com.somethingwritten.model.*;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

public class PaymentController extends HttpServlet {
	

	//processes a payment
	public void doPost( HttpServletRequest request,
						HttpServletResponse response)
						throws IOException, ServletException {
		//get the card number
		String card_num = (String)request.getParameter("card_num");
		//get the card number
		String card_name = (String)request.getParameter("card_name");
		//get the exp month
		Integer exp_month = Integer.parseInt((String)request.getParameter("exp_month"));
		//get the exp year
		Integer exp_year = Integer.parseInt((String)request.getParameter("exp_year"));
		Date card_exp = new Date();
		try{
			card_exp = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).parse(exp_year.toString()+"-"+exp_month.toString()+"-00");
		}catch(Exception dateException){
			//
		}
		
		//get the card cvv
		Integer card_cvv = Integer.parseInt((String)request.getParameter("card_cvv"));
		
		//get shipping address
		String ship_addr = (String)request.getParameter("ship_addr");
		//get shipping city
		String ship_city = (String)request.getParameter("ship_city");
		//get shipping state
		String ship_state = (String)request.getParameter("ship_state");
		//get shipping postcode
		Integer ship_pcode = Integer.parseInt((String)request.getParameter("ship_postcode"));
		
		//get billing address
		String bill_addr = (String)request.getParameter("bill_addr");
		if(bill_addr == null){
			bill_addr = ship_addr;
		}
		//get billing city
		String bill_city = (String)request.getParameter("bill_city");
		if(bill_city == null){
			bill_city = ship_city;
		}
		//get billing state
		String bill_state = (String)request.getParameter("bill_state");
		if(bill_state == null){
			bill_state = ship_state;
			
		}
		//get billing postcode
		String txt_pcode = (String)request.getParameter("bill_postcode");
		Integer bill_pcode = 0;
		if(txt_pcode == null){
			bill_pcode = ship_pcode;
		}else{
			bill_pcode = Integer.parseInt(txt_pcode);
		}
		
		//get shipping method.
		Integer ship_method_id = Integer.parseInt((String)request.getParameter("ship_method_id"));
		
		//get the existing cart if it exists in the session.
		ShoppingCart tmpCart = (ShoppingCart)request.getSession().getAttribute("cart");
		if(tmpCart == null){
			//if it's null, create a new cart.
			tmpCart = new ShoppingCart();
		}
		
		Payment thisPayment = new Payment("none", card_num, card_exp, card_cvv, card_name, bill_addr, bill_city, bill_state, bill_pcode);
		
		Order thisOrder = new Order(new Date(), ship_method_id, ship_addr, 
				ship_city, ship_state, ship_pcode, 
				thisPayment, "sent", "null", tmpCart.getAllItems());
		
		try{
			int numRows = OrderDAO.insertOrder(thisOrder);
		}catch(Exception e){
			//oh no!
		}
		
		//reset the session's cart.
		tmpCart = new ShoppingCart();
		request.getSession().setAttribute("cart", tmpCart);
		
		//forward data to the view
		RequestDispatcher view = request.getRequestDispatcher("thankyouPage.jsp");
		view.forward(request, response);
		
	}
	
	//gets the payment page
	public void doGet( HttpServletRequest request,
			HttpServletResponse response)
			throws IOException, ServletException {
		ArrayList<ShippingMethod> shipping = new ArrayList<ShippingMethod>();
		try{
			shipping = ShippingMethodDAO.getAllShippingMethods();
			
		}catch(Exception e){
			//well that sucks....
		}
		
		//update the session's cart.
		request.setAttribute("shipping", shipping);
		
		//forward data to the view
		RequestDispatcher view = request.getRequestDispatcher("paymentPage.jsp");
		view.forward(request, response);
	}
}
